package com.byb.server.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.byb.server.service.CoverGuideService;
import com.byb.server.web.rest.util.HeaderUtil;
import com.byb.server.web.rest.util.PaginationUtil;
import com.byb.server.service.dto.CoverGuideDTO;
import io.swagger.annotations.ApiParam;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * REST controller for managing CoverGuide.
 */
@RestController
@RequestMapping("/api")
public class CoverGuideResource {

    private final Logger log = LoggerFactory.getLogger(CoverGuideResource.class);

    private static final String ENTITY_NAME = "coverGuide";

    private final CoverGuideService coverGuideService;

    public CoverGuideResource(CoverGuideService coverGuideService) {
        this.coverGuideService = coverGuideService;
    }

    /**
     * POST  /cover-guides : Create a new coverGuide.
     *
     * @param coverGuideDTO the coverGuideDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new coverGuideDTO, or with status 400 (Bad Request) if the coverGuide has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/cover-guides")
    @Timed
    public ResponseEntity<CoverGuideDTO> createCoverGuide(@RequestBody CoverGuideDTO coverGuideDTO) throws URISyntaxException {
        log.debug("REST request to save CoverGuide : {}", coverGuideDTO);
        if (coverGuideDTO.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new coverGuide cannot already have an ID")).body(null);
        }
        CoverGuideDTO result = coverGuideService.save(coverGuideDTO);
        return ResponseEntity.created(new URI("/api/cover-guides/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /cover-guides : Updates an existing coverGuide.
     *
     * @param coverGuideDTO the coverGuideDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated coverGuideDTO,
     * or with status 400 (Bad Request) if the coverGuideDTO is not valid,
     * or with status 500 (Internal Server Error) if the coverGuideDTO couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/cover-guides")
    @Timed
    public ResponseEntity<CoverGuideDTO> updateCoverGuide(@RequestBody CoverGuideDTO coverGuideDTO) throws URISyntaxException {
        log.debug("REST request to update CoverGuide : {}", coverGuideDTO);
        if (coverGuideDTO.getId() == null) {
            return createCoverGuide(coverGuideDTO);
        }
        CoverGuideDTO result = coverGuideService.save(coverGuideDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, coverGuideDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /cover-guides : get all the coverGuides.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of coverGuides in body
     */
    @GetMapping("/cover-guides")
    @Timed
    public ResponseEntity<List<CoverGuideDTO>> getAllCoverGuides(@ApiParam Pageable pageable) {
        log.debug("REST request to get a page of CoverGuides");
        Page<CoverGuideDTO> page = coverGuideService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/cover-guides");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /cover-guides/:id : get the "id" coverGuide.
     *
     * @param id the id of the coverGuideDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the coverGuideDTO, or with status 404 (Not Found)
     */
    @GetMapping("/cover-guides/{id}")
    @Timed
    public ResponseEntity<CoverGuideDTO> getCoverGuide(@PathVariable Long id) {
        log.debug("REST request to get CoverGuide : {}", id);
        CoverGuideDTO coverGuideDTO = coverGuideService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(coverGuideDTO));
    }

    /**
     * DELETE  /cover-guides/:id : delete the "id" coverGuide.
     *
     * @param id the id of the coverGuideDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/cover-guides/{id}")
    @Timed
    public ResponseEntity<Void> deleteCoverGuide(@PathVariable Long id) {
        log.debug("REST request to delete CoverGuide : {}", id);
        coverGuideService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

    /**
     * SEARCH  /_search/cover-guides?query=:query : search for the coverGuide corresponding
     * to the query.
     *
     * @param query the query of the coverGuide search
     * @param pageable the pagination information
     * @return the result of the search
     */
    @GetMapping("/_search/cover-guides")
    @Timed
    public ResponseEntity<List<CoverGuideDTO>> searchCoverGuides(@RequestParam String query, @ApiParam Pageable pageable) {
        log.debug("REST request to search for a page of CoverGuides for query {}", query);
        Page<CoverGuideDTO> page = coverGuideService.search(query, pageable);
        HttpHeaders headers = PaginationUtil.generateSearchPaginationHttpHeaders(query, page, "/api/_search/cover-guides");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

}
